package view;

import entity.StudentDO;
import handler.UpdateStudentViewHandler;
import service.StudentService;
import service.impl.StudentServiceImpI;

import javax.swing.*;
import java.awt.*;

public class UpdateStudentView extends JDialog {

    JPanel jPanel = new JPanel();
    //    JLabel snoLabel = new JLabel("学号");
//    JTextField snoTxt = new JTextField();
    JLabel nameLabel = new JLabel("姓名");
    JTextField nameTxt = new JTextField();

    JLabel snoLabel = new JLabel("学号");
    JTextField snoTxt = new JTextField();

    JLabel sexLabel = new JLabel("性别");
    JTextField sexTxt = new JTextField();

    JLabel ageLabel = new JLabel("年龄");
    JTextField ageTxt = new JTextField();


    JLabel deptLabel = new JLabel("系别");
    JTextField deptTxt = new JTextField();


    JLabel speLabel = new JLabel("专业");
    JTextField speTxt = new JTextField();


    JLabel classLabel = new JLabel("班级");
    JTextField classTxt = new JTextField();

    //JButton addBtn = new JButton("添加");

    UpdateStudentViewHandler updateStudentViewHandler;
    //    JPanel jPanel = new JPanel();
//    JLabel snoLabel = new JLabel("编号");
//    JTextField snoTxt = new JTextField();
//    JLabel nameLabel = new JLabel("姓名");
//    JTextField nameTxt = new JTextField();
//    JLabel noLabel = new JLabel("学号");
//    JTextField noTxt = new JTextField();
//    JLabel course = new JLabel("课程");
//    JTextField couTxt = new JTextField();
//    JLabel grade = new JLabel("成绩");
//    JTextField gradeTxt = new JTextField();
//
    JButton updateBtn = new JButton("修改");
//
//    UpdateStudentViewHandler updateStudentViewHandler;

    public UpdateStudentView(MainView mainView, int selectStudentId) {
        super(mainView, "修改信息", true);
        //设置监听
        updateStudentViewHandler = new UpdateStudentViewHandler(this, mainView);
        //查询对应记录并回显
        StudentService studentService = new StudentServiceImpI();
        StudentDO selectStu = studentService.getById(selectStudentId);

        snoLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(snoLabel);
        snoTxt.setPreferredSize(new Dimension(200, 30));
        snoTxt.setText(selectStu.getSno() + "");
        //id设置为不可编辑
        snoTxt.setEnabled(false);
        jPanel.add(snoTxt);

        nameLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(nameLabel);
        nameTxt.setPreferredSize(new Dimension(200, 30));
        nameTxt.setText(selectStu.getSname());
        //nameTxt.setEnabled(false);
        jPanel.add(nameTxt);

        sexLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(sexLabel);
        sexTxt.setPreferredSize(new Dimension(200, 30));
        sexTxt.setText(selectStu.getSex());
        //sexTxt.setEnabled(false);
        jPanel.add(sexTxt);

        ageLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(ageLabel);
        ageTxt.setPreferredSize(new Dimension(200, 30));
        ageTxt.setText(String.valueOf(selectStu.getAge()));
        //ageTxt.setEnabled(false);
        jPanel.add(ageTxt);

        deptLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(deptLabel);
        deptTxt.setPreferredSize(new Dimension(200, 30));
        deptTxt.setText(selectStu.getDept());
        //deptTxt.setEnabled(false);
        jPanel.add(deptTxt);

        speLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(speLabel);
        speTxt.setPreferredSize(new Dimension(200, 30));
        speTxt.setText(selectStu.getSpe());
        //speTxt.setEnabled(false);
        jPanel.add(speTxt);

        classLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(classLabel);
        classTxt.setPreferredSize(new Dimension(200, 30));
//        classTxt.setText(String.valueOf(selectStu.getClass()));//错的
        classTxt.setText(selectStu.getSclass());
        //classTxt.setEnabled(false);
        jPanel.add(classTxt);

        updateBtn.addActionListener(updateStudentViewHandler);
        jPanel.add(updateBtn);

        Container contenPane = getContentPane();
        contenPane.add(jPanel);

        setSize(600, 400);//大小
        setVisible(true);//可以看见
        setResizable(false);//不可变大小
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);//退出自动关闭
        setLocationRelativeTo(null);//居中
    }

    //获取修改后的学生对象
    public StudentDO buildUpdateStudentDO() {
        StudentDO studentDO = new StudentDO();
        studentDO.setSno(Integer.valueOf(snoTxt.getText()));
        studentDO.setSname(nameTxt.getText());
        studentDO.setSex(sexTxt.getText());
        studentDO.setAge(ageTxt.getText());
        studentDO.setDept(deptTxt.getText());
        studentDO.setSpe(speTxt.getText());
        studentDO.setSclass(classTxt.getText());

//        StudentDO studentDO =new StudentDO();
//        studentDO.setSno(Integer.valueOf(idTxt.getText()));
//        studentDO.setSname(nameTxt.getText());
//        studentDO.setSname(noTxt.getText());
//        studentDO.setSname(couTxt.getText());
//        studentDO.setSname(gradeTxt.getText());
        return studentDO;
    }
}
